package cc.fs.zhihuapp.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.nekocode.gallery.AnimationRect;
import com.nekocode.gallery.GalleryAnimationActivity;

import java.util.ArrayList;

import cc.fs.zhihuapp.R;
import cc.fs.zhihuapp.model.News;


/**
 * Created by fostion on 2015/9/7.
 */
public class MyViewPagerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int TYPE_ITEM = 0;
    public static final int TYPE_HEAD = 1;
    public static final int TYPE_TAG = 2;
    public static final int TYPE_LOAD_MORE = 3;

    private ArrayList<News> list;
    private boolean isLoadMore;
    private OnItemClick onItemClick;

    public MyViewPagerAdapter(ArrayList<News> list) {
        this.list = list;
    }

//    var onNewsItemClick: ((News) -> Unit)? = null
//    var onScrollToTag: ((News) -> Unit)? = null
//    var onNewsItemLongClickListener: ((News) -> Boolean)? = null


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        switch (viewType) {
            case TYPE_ITEM: {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_detail, parent, false);
                return new ItemViewHolder(v);
            }
            case TYPE_LOAD_MORE: {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_footer_loadingmore, parent, false);
                return new LoadMoreView(v);
            }
            case TYPE_TAG: {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_section, parent, false);
                return new TagViewHolder(v);
            }
            case TYPE_HEAD: {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_head_myviewpager_empty, parent, false);
                return new HeadView(v);
            }
        }

        throw new UnsupportedOperationException();
    }

    @Override
    public int getItemCount() {
        if (isLoadMore) {
            return list.size() + 2;
        }
        return list.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return TYPE_HEAD;
            case 1: {
                if (list.size() == 0) return TYPE_LOAD_MORE;
                else return TYPE_TAG;
            }

            default: {
                if (position == list.size() + 1) {
                    if (isLoadMore) return TYPE_LOAD_MORE;
                    else return TYPE_ITEM;
                }

                int index = position - 1;
                if (list.get(index).isTag()) return TYPE_TAG;
            }
            break;
        }
        return TYPE_ITEM;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int index = position - 1;
        if (holder instanceof ItemViewHolder) {
            ItemViewHolder vh = (ItemViewHolder) holder;
            vh.setData(list.get(index));
        }

        if (holder instanceof TagViewHolder) {
            TagViewHolder vh = (TagViewHolder) holder;
            vh.setData(list.get(index));
        }
    }

    public void setIsLoadMore(boolean isLoadMore) {
        this.isLoadMore = isLoadMore;
        notifyDataSetChanged();
    }

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    static class TagViewHolder extends RecyclerView.ViewHolder {

        TextView title;

        public TagViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
        }

        public void setData(News news) {
            title.setText(news.getTitle());
        }
    }

    static class HeadView extends RecyclerView.ViewHolder {

        public HeadView(View itemView) {
            super(itemView);
        }
    }

    static class LoadMoreView extends RecyclerView.ViewHolder {

        public LoadMoreView(View itemView) {
            super(itemView);
        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {

        SimpleDraweeView image;// by bindView(R.id.image)
        TextView title;// by bindView(R.id.title)

        public ItemViewHolder(View itemView) {
            super(itemView);
            image = (SimpleDraweeView) itemView.findViewById(R.id.image);
            title = (TextView) itemView.findViewById(R.id.title);
        }

        public void setData(final News news) {
            title.setText(news.getTitle());
            image.setImageURI(Uri.parse(news.getImages()[0]));
            image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Context context = v.getContext();
                    ArrayList<AnimationRect> animationRectArrayList = new ArrayList<AnimationRect>();
                    AnimationRect rect = AnimationRect .buildFromImageView(context, (ImageView) v);
                    animationRectArrayList.add(rect);

                    ArrayList<String> thumbnailUrlsList = new ArrayList<String>();
                    thumbnailUrlsList.add(news.getImages()[0]);

                    Intent intent = GalleryAnimationActivity.newIntent(context, thumbnailUrlsList, animationRectArrayList, 0);
                    context.startActivity(intent);
                }
            });
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClick != null) {
                        onItemClick.onItemClick(news);
                    }
                }
            });
        }
    }

    public interface OnItemClick {
        void onItemClick(News news);
    }
}
